﻿using System;
using System.Collections.Generic;
using System.Linq;
using AMM.CustomClassLibrary;
using AMM.DataAccess.Store;
using AMM.Model;
using AMM.Model.Store;

namespace AMM.Business.Store {
    public class QuotationProcessor : BaseProcessor {

        private int _pageSize;
        private QuotationRepository _quotationRepository;

        public QuotationProcessor(int pageSize = int.MaxValue) {
            this._pageSize = pageSize;
            this._quotationRepository = new QuotationRepository(pageSize);
        }

        public Quotation FetchQuotationById(int quotationId) {
            try {
                return this._quotationRepository.FetchQuotationById(quotationId);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取指定物料报价资料失败！");
            }
        }

        public List<Quotation> FetchQuotationList() {
            try {
                return this._quotationRepository.FetchQuotationList();
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取物料报价列表失败！");
            }
        }

        public Quotation FetchQuotationByVendorIdAndMaterialId(int vendorId, int materialId) {
            try {
                return this._quotationRepository.FetchQuotationsByVendorId(vendorId)
                    .Where(x => x.Status == 2)
                    .FirstOrDefault(x => x.MaterialId == materialId);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取物料报价单失败！");
            }
        }

        public List<QuotationViewModel> FetchQuotationViewModelList(QuotationSearchModel searchModel, int pageIndex = 1) {
            try {
                List<Quotation> resultList = this._quotationRepository.FetchQuotationList(searchModel, pageIndex);
                return resultList.Select(x => {
                    return new QuotationViewModel() {
                        Key = x.QuotationId,
                        VendorCode = x.Vendor.Code,
                        VendorName = x.Vendor.Name,
                        MaterialCode = x.Material.Code,
                        Price = x.Price,
                        PriceThresholdValue = x.PriceThresholdValue,
                        PriceGreaterThan = x.PriceGreaterThan,
                        MinimumPackage = x.MinimumPackage,
                        MinimumProcurement = x.MinimumProcurement,
                        PreProcurementCycle = x.PreProcurementCycle,
                        ProcurementCycle = x.ProcurementCycle,
                        IsMultiple = x.IsMultiple,
                        Unit = x.Basic.Code,
                        IsAttach = x.AttachmentId.HasValue,
                        OrderStatus = MethodHelper.GetEnumsDescription(typeof(AuditStatus), x.Status),
                        AuditedBy = x.AuditedBy,
                        AuditedDatetime = x.AuditedDatetime,
                        CreatedBy = x.CreatedBy,
                        CreatedDatetime = x.CreatedDatetime,
                        ModifiedBy = x.ModifiedBy,
                        ModifiedDatetime = x.ModifiedDatetime
                    };
                }).ToList();
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("获取物料报价列表失败！");
            }
        }

        public void Insert(Quotation quotation) {
            try {
                quotation.QuotationId = Guid.NewGuid().GetHashCode();
                quotation.CreatedDatetime = DateTime.Now;
                this._quotationRepository.Insert(quotation);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("插入新物料报价失败！");
            }
        }

        public void Update(Quotation quotation) {
            try {
                Quotation oldQuotation = this.FetchQuotationById(quotation.QuotationId);
                if (!oldQuotation.RowVersion.SequenceEqual(quotation.RowVersion)) {
                    throw this.BuildException("更新物料报价失败，数据不一致性错误！");
                }
                quotation.ModifiedDatetime = DateTime.Now;
                this._quotationRepository.Update(quotation);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("更新物料报价失败！");
            }
        }

        public void Delete(Quotation quotation) {
            try {
                this._quotationRepository.Delete(quotation);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("删除物料报价失败！");
            }
        }

        public void Delete(params int[] quotationIds) {
            try {
                this._quotationRepository.DeleteMultipleQuotation(quotationIds);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("删除物料报价失败！");
            }
        }

        public void Audit(Quotation quotation) {
            try {
                Quotation oldQuotation = this.FetchQuotationById(quotation.QuotationId);
                if (!oldQuotation.RowVersion.SequenceEqual(quotation.RowVersion)) {
                    throw this.BuildException("审核物料报价失败，数据不一致性错误！");
                }
                quotation.AuditedDatetime = DateTime.Now;
                this._quotationRepository.Update(quotation);
            }
            catch (Exception ex) {
                Logger.Error(ex.Message, ex);
                throw this.BuildException("审核物料报价失败！");
            }
        }
    }
}
